#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


#define NUM 5
typedef void (*fun_t)();
fun_t tasks[NUM];

void printLog()
{
  printf("this is a log print task\n");
}

void printNet()
{
  printf("this is a net task\n");
}

void printNPC()
{
  printf("this is a flush NPC\n");
}

void initTask()
{
  tasks[0]=printLog;
  tasks[1]=printNet;
  tasks[2]=printNPC;
  tasks[3]=NULL;
}

void execteTask()
{
  for(int i=0;tasks[i];i++)
  {
    tasks[i]();
  }
}

int main()
{
  initTask();
  pid_t id = fork();
  if(id == 0)
  {
    int cnt = 5;
    while(cnt)
    {
      printf("I am child process,pid:%d,ppid:%d\n",getpid(),getppid());
      sleep(1);
      cnt--;
    }
    exit(1);
  }
  int status = 0;
  while(1)
  {
    pid_t rid = waitpid(id,&status,WNOHANG);
    if(rid > 0)
    {
      printf("wait success,rid:%d,status:%d\n",rid,status);
    }
    else if(rid == 0)
    {
      printf("child is running\n");
      execteTask();
      printf("-----------------------------------------\n");
    }
    else 
    {
      perror("waitpid");
      break;
    }
    sleep(1);
  }
  return 0;
}
